import random
import unittest
import KBuildDB
import KQueryDB
import datetime
import time

class TestSequenceFunctions(unittest.TestCase):
    def setUp(self):    
        self.db=KBuildDB.KBuildDB()
        self.kdb=KQueryDB.KQueryDB()
    @unittest.skip("")
    def test_ReBuildAllEODHistory(self):
        self.db.CleanUp()
        self.db.BuildAllEODHistory('D:\\Dropbox\\rukun\\Trading\\KunTrading\\Data\\EOD')
        pass
    def test_BuildAllStooqHistory(self):
        self.db.ImportHistoricalStooq("D:\\Dropbox\\Trading2013\\Data\\stooq")
    @unittest.skip("")
    def test_QueryStockList(self):
        startdate=datetime.datetime(2011, 11, 10)
        endate=datetime.datetime(2012, 11, 10)        
        allstocks=self.kdb.QueryAllStocksByMkt("NYSE")
        self.assertGreater(len(allstocks),3000)
    @unittest.skip("")
    def test_QueryAllStocksByDateRange(self):
        start=time.clock()
        index=0
        market="NYSE"
        allstocks=self.kdb.QueryAllStocksByMkt("NYSE")
        startdate=datetime.datetime(2009, 11, 10)
        endate=datetime.datetime(2013, 11, 10)
        for each_stock in allstocks:
            self.kdb.QueryStockByDateRange(market,each_stock,startdate,endate) 
            elapsed=(time.clock()-start)
            index=index+1
            print('stock# %d; time %.3f' %(index,elapsed))  
        self.assertLess(elapsed,300)
    if __name__ == '__main__':
        unittest.main()

